package com.yoomiito.app.adapter.cicle;

import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.cicle.ImageBean;

import java.io.File;
import java.util.List;

public class CiclePublishImageAdapter extends BaseQuickAdapter<ImageBean, BaseViewHolder> {
    public CiclePublishImageAdapter(@Nullable List<ImageBean> data) {
        super(R.layout.item_cicle_publish_photo, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, ImageBean item) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.goods_default);

        if (item.isAdd()){
            helper.setVisible(R.id.iv_close,false);
            ((ImageView)helper.getView(R.id.iv_photo)).setImageResource(R.mipmap.image_add);
        }else{
                if (!TextUtils.isEmpty(item.getImagePath())){
                    File file = new File(item.getImagePath()) ;
                    Glide.with(App.getApplication()).load(file).apply(requestOptions).into((ImageView)helper.getView(R.id.iv_photo));
                }else if (!TextUtils.isEmpty(item.getUrl())){
                    Glide.with(App.getApplication()).load(item.getUrl()).apply(requestOptions).into((ImageView)helper.getView(R.id.iv_photo));
                }
            helper.setVisible(R.id.iv_close,true);

            helper.setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRemoveListener != null){
                        onRemoveListener.onRemove(v,item);
                    }
                }
            });
        }
    }

    @Override
    public void setOnItemChildClickListener(OnItemChildClickListener listener) {
        super.setOnItemChildClickListener(listener);
    }

    private OnRemoveListener onRemoveListener;

    public void setOnRemoveListener(OnRemoveListener onRemoveListener) {
        this.onRemoveListener = onRemoveListener;
    }

    public interface OnRemoveListener{
        public void onRemove(View v,ImageBean bean);
    }

}
